package com.tianji.learning.service.impl;

import cn.hutool.core.date.DateUtil;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.mapper.PointsBoardSeasonMapper;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author taohongrun
 * @since 2025-02-11
 */
@Service
@Slf4j
public class PointsBoardSeasonServiceImpl extends ServiceImpl<PointsBoardSeasonMapper, PointsBoardSeason> implements IPointsBoardSeasonService {
    @Override
    public Integer querySeasonByTime(LocalDate time) {
        //time为上个月第一天
        LocalDate lastDayOfLastMonth = time.plusMonths(1).plusDays(-1);

        PointsBoardSeason pointsBoardSeason = lambdaQuery()
                .eq(PointsBoardSeason::getBeginTime, time)
                .one();
        if(pointsBoardSeason == null) {
            log.info("赛季内容不存在，创造对应时间赛季：{}", time);
            PointsBoardSeason pointsBoardSeason1 = new PointsBoardSeason();
            pointsBoardSeason1.setBeginTime(time);
            pointsBoardSeason1.setEndTime(lastDayOfLastMonth);
            Integer id = lambdaQuery().orderByDesc(PointsBoardSeason::getBeginTime).last("limit 1").one().getId();
            int i = id + 1;
            pointsBoardSeason1.setName("第"+i+"赛季");
            log.info("对应赛季信息:{}", pointsBoardSeason1);
            save(pointsBoardSeason1);
            return pointsBoardSeason1.getId();
        }
        log.info("赛季内容存在，直接返回对应赛季：{}", pointsBoardSeason);
        return pointsBoardSeason.getId();
    }
}
